package com.example.JPetStore.controller;

import com.example.JPetStore.Common;
import com.example.JPetStore.dao.AddressDao;
import com.example.JPetStore.dao.CartDao;
import com.example.JPetStore.dao.ItemDao;
import com.example.JPetStore.dao.OrderDao;
import com.example.JPetStore.domain.*;
import com.example.JPetStore.dto.CartDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.ArrayList;

@RequestMapping("/cart")
@Controller
public class CartController {
    @Autowired
    AddressDao addressDao;
    @Autowired
    ItemDao itemDao;
    @Autowired
    CartDao cartDao;
    @Autowired
    OrderDao orderDao;
    @PostMapping("/AddAddressController")
    public ModelAndView addAddress(HttpSession session, @RequestParam("address")String addressContent){
        ModelAndView mv=new ModelAndView();
        User user= Common.checkLogin(mv,session);
        if(user==null)return mv;

        if(addressContent.equals("")){
            Common.sendErrorMessage(mv, "未输入地址内容", "/cart/AddressController");
            return mv;
        }

        addressDao.addAddress(addressContent,user.getUserId());
        Common.sendErrorMessage(mv, "添加成功！", "/cart/AddressController");
        return mv;
    }

    @GetMapping("/AddressController")
    public ModelAndView Address(HttpSession session){
        ModelAndView mv=new ModelAndView();
        User user= Common.checkLogin(mv,session);
        if(user==null)return mv;


        ArrayList<Address> addresses=addressDao.queryAddresses(user);

        mv.addObject("addresses",addresses);
        mv.setViewName("/cart/address");
        return mv;
    }

    @GetMapping("/AddToCartController")
    public ModelAndView addToController(HttpSession session,@RequestParam("item_id")String itemId){
        ModelAndView mv=new ModelAndView();
        User user= Common.checkLogin(mv,session);
        if(user==null)return mv;

        if(itemId.equals("")){
            Common.sendErrorMessage(mv, "未输入itemId！", "/catalog/MainController");
            return mv;
        }

        Item item= itemDao.queryItemById(itemId);

        if(item==null){
            Common.sendErrorMessage(mv, "item不存在！", "/catalog/MainController");
            return mv;
        }

        cartDao.addToCart(itemId,user.getUserId());

        Common.sendErrorMessage(mv, "添加成功！", "/cart/CartController");
        return mv;
    }

    @PostMapping("/BuyCartActionController")
    public ModelAndView buyCartAction(HttpSession session,@RequestParam("cart_id")String cartId,@RequestParam("address")String address,
                                      @RequestParam("order_phone")String orderPhone,@RequestParam("delivery_name")String deliveryName,
                                      @RequestParam("item_number")String itemNumber,@RequestParam("payment_method")String paymentMethod){
        ModelAndView mv=new ModelAndView();

        User user= Common.checkLogin(mv,session);
        if(user==null)return mv;

        if(cartId==null||orderPhone==null||deliveryName==null||address==null||itemNumber==null||paymentMethod==null){
            Common.sendErrorMessage(mv, "缺少参数！", "/catalog/MainController");
            return mv;
        }

        CartDto cart=cartDao.queryCartById(cartId,user.getUserId());

        if(cart==null){
            Common.sendErrorMessage(mv, "购物车数据不存在！", "/catalog/MainController");
            return mv;
        }


//        Order order=new Order();
//
//        order.setAddress(address);
//        order.setOrderPhone(orderPhone);
//        order.setDeliveryName(deliveryName);
//        order.setUserId((cart.getUserId()+""));
//        order.setItemId(cart.getItemId());
//        order.setItemIntroduction(cart.getItemIntroduction());
//        order.setItemPrice(cart.getItemPrice());
//        order.setItemNumber(itemNumber);
//        order.setPaymentMethod(paymentMethod);
//        order.setTotalPrice((new BigDecimal(cart.itemPrice)).multiply(new BigDecimal(itemNumber)).toString());
//
//        orderDao.addOrder(order);
//
//        cartDao.removeCart(cartId, user.getUserId());

        Common.sendErrorMessage(mv, "即将进入支付界面", "/PayController/goAlipay");
        return mv;
    }

    @GetMapping("/BuyCartController")
    public ModelAndView buyCart(HttpSession session,@RequestParam("cart_id")String cartId,@RequestParam("item_number")String itemNumber){
        ModelAndView mv=new ModelAndView();
        User user= Common.checkLogin(mv,session);
        if(user==null)return mv;

        CartDto cart=cartDao.queryCartById(cartId,user.getUserId());

        if(cart==null){
            Common.sendErrorMessage(mv, "购物车数据不存在！", "/cart/CartController");
            return mv;
        }

        mv.addObject("cart",cart);
        mv.addObject("itemNumber",itemNumber);
        ArrayList<Address> addresses= addressDao.queryAddresses(user);
        mv.addObject("addresses",addresses);

        String[] paymentMethods={"支付宝支付","微信支付","银行卡支付",};
        mv.addObject("paymentMethods",paymentMethods);
        if(addresses.size()==0){
            mv.setViewName("redirect:/cart/AddressController");
            return mv;
        }
        mv.setViewName("/cart/buycart");
        return mv;
    }

    @GetMapping("/CartController")
    public ModelAndView Cart(HttpSession session){
        ModelAndView mv=new ModelAndView();
        User user= Common.checkLogin(mv,session);
        if(user==null)return mv;

        ArrayList<CartDto> carts= cartDao.queryCarts(user);


        mv.addObject("carts",carts);
        mv.setViewName("cart/cart");
        return mv;
    }

    @GetMapping("/RemoveAddressController")
    public ModelAndView removeAddress(HttpSession session,@RequestParam("address_id")String addressId){
        ModelAndView mv=new ModelAndView();
        User user= Common.checkLogin(mv,session);
        if(user==null)return mv;

        Address address=addressDao.queryAddressById(addressId,user.getUserId());
        System.out.println(address.getAddressId());
        if(address==null){
            Common.sendErrorMessage(mv, "地址数据不存在！", "/cart/AddressController");
            return mv;
        }
        addressDao.removeAddress(addressId,user.getUserId());
        Common.sendErrorMessage(mv, "移除成功！", "/cart/AddressController");
        return mv;
    }

    @GetMapping("/RemoveCartController")
    public ModelAndView removeCart(HttpSession session,@RequestParam("cart_id")String cartId){
        ModelAndView mv=new ModelAndView();
        User user= Common.checkLogin(mv,session);
        if(user==null)return mv;

        CartDto cart=cartDao.queryCartById(cartId,user.getUserId());
        if(cart==null){
            Common.sendErrorMessage(mv, "购物车数据不存在！", "/cart/CartController");
            return mv;
        }
        cartDao.removeCart(cartId,user.getUserId());
        Common.sendErrorMessage(mv, "移除成功！", "/cart/CartController");
        return mv;
    }


}
